package com.example.administrator.kotiln.ui;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.base.BaseActivity;
import com.example.administrator.kotiln.utils.Share;

/**
 * Created by Administrator on 2017/10/18.
 */

public class AnimatorActivity extends BaseActivity {

    private ImageView myView;
    private Context mContext;
    private ProgressBar progress;
    private TimeCount time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        mContext = AnimatorActivity.this;
        initView();
        RotateAnimation();
        AlpahAnimation();
        initTo();


    }

    @Override
    protected void onResume() {
        super.onResume();
        progress.setVisibility(View.VISIBLE);
        initTime();
    }

    private void initView() {
        myView = (ImageView) findViewById(R.id.myView);
        progress = (ProgressBar) findViewById(R.id.progress);
    }

    private void RotateAnimation() {

//        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale);
//        myView.startAnimation(animation);


        Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.alpha_anim);
        myView = (ImageView) findViewById(R.id.myView);
        myView.startAnimation(animation);
//


//
//        ObjectAnimator anim = ObjectAnimator.ofFloat(myView, "rotation", 0f, 360f);
//        anim.setDuration(1000);
//        anim.setInterpolator(new DecelerateInterpolator());//设置动画插入器，减速
//        anim.setRepeatCount(-1);//设置动画重复次数，这里-1代表无限
//        anim.setRepeatMode(Animation.REVERSE);//设置动画循环模式。
//        anim.start();
    }


    private void AlpahAnimation() {
        ObjectAnimator anim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.8f, 0.6f, 0.4f, 0.2f, 0.0f);
        anim.setRepeatCount(-1);
        anim.setRepeatMode(ObjectAnimator.REVERSE);
        anim.setDuration(2000);
        anim.start();
    }

    private void initTo() {
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(myView, "alpha", 1.0f, 0.5f, 0.8f, 1.0f);
        ObjectAnimator scaleXAnim = ObjectAnimator.ofFloat(myView, "scaleX", 0.0f, 1.0f);
        ObjectAnimator scaleYAnim = ObjectAnimator.ofFloat(myView, "scaleY", 0.0f, 2.0f);
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(myView, "rotation", 0, 360);
        ObjectAnimator transXAnim = ObjectAnimator.ofFloat(myView, "translationX", 100, 400);
        ObjectAnimator transYAnim = ObjectAnimator.ofFloat(myView, "tranlsationY", 100, 750);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
//                set.playSequentially(alphaAnim, scaleXAnim, scaleYAnim, rotateAnim, transXAnim, transYAnim);
        set.setDuration(3000);
        set.start();
    }


    private void initTime() {
        // 构造CountDownTimer对象
        time = new TimeCount(5000, 1000);
        time.start();
    }

    /**
     * 倒计时
     *
     * @author Administrator
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onTick(long l) {
            // 计时过程显示
        }

        @Override
        public void onFinish() {
            // 计时完毕时触发
            progress.setVisibility(View.GONE);

        }
    }
}
